﻿// 新增
function AddChild() {
    var departmentID = $("#departmentID").val();
    if (departmentID == '') {
        departmentID = 0;
    }
    var page = "/department/Edit?parentID=" + departmentID;

    OpenPop({
        id: 'child1',
        page: page,
        title: '新增/编辑组织架构',
        width: 500,
        height: 300
    });
}

// 编辑或删除操作后直接定位到上次点击操作的树菜单
var departmentID = getPara();
if (departmentID != '') {
    getChildDepartments(departmentID); // 获取子部门列表
}

$.ajax({
    url: "/department/GetChildDepartment?rd=" + new Date(),
    type: "get",
    success: function (data) {
        $("#checkBoxTree1").treeview({
            type: "normal", 						//类型，默认情况下是文字，值分别有：normal/checkbox/radio
            collapsed: false, 					//是否默认收紧，true为收紧，false为展开
            unique: true, 						//是否只允许打开一个一级节点，true为是，false为否
            checkboxCallBack: function (treeNode) {		//点击radio时的回调函数
                //console.log(treeNode);			//treeNode当前点击的radio或checkbox的JQ对象
            },
            nodeCallBack: function (treeNode) {
                var departmentID = treeNode.attr('nodevalue');
                $("#departmentID").val(departmentID); // 保存部门ID

                getChildDepartments(departmentID); // 获取子部门列表
            },
            json: data

        });

        $('#iframe1').height($('#checkBoxTree1').height());
    }
});

// 获取子部门
function getChildDepartments(departmentID) {
    $.ajax({
        url: "/department/ChildDepartmentList?departmentID=" + departmentID + "&rd=" + new Date().getMilliseconds(),
        type: "get",
        success: function (data) {
            var tableHTML = [];
            tableHTML.push('<table class="tb-data"><tr><th style="width:400px;">部门名称</th><th style="width:160px;">排序值</th><th>操作</th></tr>');
            if (data.length == 0) {
                tableHTML.push('<tr><td colspan="3">暂无下属部门!</td></tr>');
            }
            else {
                for (var i = 0; i < data.length; i++) {
                    tableHTML.push('<tr><td>' + data[i].Name + '</td><td>', data[i].SortOrder, '</td><td>' +
                    '<a href="javascript:;" onclick="Edit(' + data[i].DepartmentID + ',' + data[i].ParentID + ')">编辑</a>' +
                    ' &nbsp;&nbsp;<a href="javascript:;" onclick="Delete(' + data[i].DepartmentID + ',' + data[i].ParentID + ')">删除</a> </td></tr>');
                }
            }
            tableHTML.push('</table>');
            $('#childDepartment').html(tableHTML.join(''));
        }
    })
}

// 编辑
function Edit(departmentID, parentID) {
    OpenPop({
    	page: "/department/edit?departmentID=" + departmentID + "&parentID=" + parentID,
        title: '新增/编辑组织架构',
        width: 500,
        height: 300
    });
}

// 删除
function Delete(departmentID, parentDepartmentID) {
    // 查询是否有子类

    if (confirm('确定删除吗?')) {
        $.ajax({
            url: "/department/Delete?departmentID=" + departmentID,
            type: "POST",
            success: function (data) {
                if (data.status == "warning" || data.status == "error") {
                    alert(data.msg);
                    return false;
                }
                window.location = '/department/index?departmentID=' + parentDepartmentID; // 重载
            }
        })
    }
}

function getPara() {
    var url = location.search;
    var index = url.indexOf('?departmentID=');
    if (index != -1) {
        return url.substr(index + 14, url.length - index - 14);
    }
    return "";
}